package util;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/** WARNING: 
 *  During refactoring in accordance with Strategy pattern (for better support of multiple 
 *  paper databases), the functionality of this class migrated to papers.extractor.PubMedDB. 
 *  Thus, this class is no longer for use and will be completely removed afterwards 
 *  **/
public class PubMedParserUtil {
	public static final String PUBMED_RESOURCE_URL = "http://www.ncbi.nlm.nih.gov/pubmed/";
	/**
	 * The method verifies, whether the pubmed resource contains an associated PMC article
	 * and, if true, returns the HTTP link to it
	 * Note: all the links to PMC resources contain ref="PrId=3494..." attribute. We use this
	 * feature for verification
	 * @param pubmedID - id of the resource at http://www.ncbi.nlm.nih.gov/pubmed/{id}
	 * @return either the link to the document or null, if the PMC article is not available
	 */
	@SuppressWarnings("static-access")
	public static String extractPMCArticleLink(int pubmedID) {
		String PMCLink = null;
		Document doc;
		try {
			doc = Jsoup.connect(PUBMED_RESOURCE_URL+pubmedID).get();
			Elements links = doc.select("a");
			for (Element l: links) {
				String ref = l.attr("ref");
				if (ref!=null && ref.startsWith("PrId=3494")) {
					PMCLink = l.attr("href");
					break;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return PMCLink;
	}
}
